package com.punai.web.controller.cs;

import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.entity.SysUser;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.system.domain.CsLinkUserRole;
import com.punai.system.service.CsRoleLinkUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/cs/roleLinkUser")
@RestController
public class CsRoleLinkUserController extends BaseController {

    @Autowired
    private CsRoleLinkUserService csRoleLinkUserService;
    @PreAuthorize("@ss.hasPermi('cs:roleLinkUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUser sysUser)
    {
        startPage();
        Long companyId = getCompanyId();
        sysUser.setCompanyId(companyId);
        List<SysUser> list = csRoleLinkUserService.selectLinkUserList(sysUser);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('cs:roleLinkUser:edit')")
    @PutMapping("/cancelAll")
    public AjaxResult cancelAll(Long roleId, Long[] userIds)
    {
        return toAjax(csRoleLinkUserService.cancelAll(roleId,userIds,getCompanyId()));
    }

    @PreAuthorize("@ss.hasPermi('cs:roleLinkUser:edit')")
    @PutMapping("/cancel")
    public AjaxResult cancel(CsLinkUserRole csLinkUserRole)
    {
        csLinkUserRole.setCompanyId(getCompanyId());
        return toAjax(csRoleLinkUserService.cancel(csLinkUserRole));
    }

    @PreAuthorize("@ss.hasPermi('cs:roleLinkUser:edit')")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/grantCsRole")
    public AjaxResult grantCsRole(String roleId, Long[] userIds)
    {
        return toAjax(csRoleLinkUserService.insertAuthUsers(roleId, userIds,getCompanyId()));
    }


    @PreAuthorize("@ss.hasPermi('cs:roleLinkUser:list')")
    @GetMapping("/unallocatedList")
    public TableDataInfo unallocatedList(SysUser user)
    {
        startPage();
        List<SysUser> list = csRoleLinkUserService.selectUnallocatedList(user);
        return getDataTable(list);
    }
}
